﻿#include "FITKGraphObjectGeoPart.h"

// VTK
#include <vtkUnstructuredGrid.h>
#include <vtkPolyData.h>

// Graph 
#include "FITKGraphCommons.h"
#include "FITKPartDataSetManager.h"
#include "FITKGraphObjectModelHighlighting.h"
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"

// Data
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"

namespace Exchange
{
    FITKGraphObjectGeoPart::FITKGraphObjectGeoPart(AbaqusData::FITKDataCase* caseData, AbaqusData::FITKAbaqusPart* part)
        : FITKGraphObjectGeoModelBase(caseData, part)
    {
        if (!part)
        {
            return;
        }

        // Initialize.
        init();

        // Generate the shape data and actors.
        generateGraph();

        // Save the data type.
        m_graphInfo.Type = GraphDataType::GeometryPartGraph;
        m_graphInfo.PartId = part->getDataObjectID();
    }

    void FITKGraphObjectGeoPart::init()
    {
        this->Superclass::init();

        AbaqusData::FITKAbaqusPart* part = getPartData();
        if (!part)
        {
            return;
        }

        PartGeomDataVTK* data{ nullptr };
        bool flag = FITKPartDataSetManager::getInstance()->getPartGeomData(part->getDataObjectID(), data);
        if (!flag)
        {
            return;
        }

        // Initialize the highlightor.
        m_graphObjHighlight->setModelInputDataObject(data->DataSetEdge);
        m_graphObjPreHighlight->setModelInputDataObject(data->DataSetEdge);
    }

    //void FITKGraphObjectGeoPart::update(bool forceUpdate)
    //{
    //    AbaqusData::FITKAbaqusPart* part = getPartData();
    //    if (!part)
    //    {
    //        return;
    //    }

    //    // Try to update the datasets.
    //    this->Superclass::update(forceUpdate);

    //    // Initialize the input datasets.
    //    PartGeomDataVTK* data{ nullptr };
    //    bool flag = FITKPartDataSetManager::getInstance()->getPartGeomData(part->getDataObjectID(), data);
    //    if (flag)
    //    {
    //        m_fActorVertex->setInputDataObject(data->DataSetVertex);
    //        m_fActorEdge->setInputDataObject(data->DataSetEdge);
    //        m_fActorFace->setInputDataObject(data->DataSetFace);
    //    }
    //}

    AbaqusData::FITKAbaqusPart* FITKGraphObjectGeoPart::getPartData()
    {
        AbaqusData::FITKAbaqusPart* part = dynamic_cast<AbaqusData::FITKAbaqusPart*>(_dataObj);
        return part;
    }

    void FITKGraphObjectGeoPart::generateGraph()
    {
        // Create the actors.
        this->Superclass::generateGraph();

        if (!m_fActorFace || !m_fActorEdge || !m_fActorVertex)
        {
            return;
        }

        AbaqusData::FITKAbaqusPart* part = getPartData();
        if (!part)
        {
            return;
        }

        PartGeomDataVTK* data{ nullptr };
        bool flag = FITKPartDataSetManager::getInstance()->getPartGeomData(part->getDataObjectID(), data);
        if (!flag)
        {
            return;
        }

        // Initialize the input datasets.
        m_fActorVertex->setInputDataObject(data->DataSetVertex);
        m_fActorEdge->setInputDataObject(data->DataSetEdge);
        m_fActorFace->setInputDataObject(data->DataSetFace);

        // Set face color.
        m_fActorFace->setColor(FITKGraphCommons::s_geometryPartColor);
    }

    void FITKGraphObjectGeoPart::colorByDefault(bool forceUpdate)
    {
        Q_UNUSED(forceUpdate);

        if (!m_fActorFace)
        {
            return;
        }

        // Set face color.
        m_fActorFace->setColor(FITKGraphCommons::s_geometryPartColor);

        // Save the status.
        m_colorMode = ColorMode::ColorDefault;
    }

    void FITKGraphObjectGeoPart::colorByDefaultMesh(bool forceUpdate)
    {
        Q_UNUSED(forceUpdate);

        AbaqusData::FITKAbaqusPart* part = getPartData();
        if (!part || !m_fActorFace)
        {
            return;
        }

        // Set face color.
        m_fActorFace->setColor(FITKGraphCommons::s_geoPartForMeshColorFree);

        // Save the status.
        m_colorMode = ColorMode::ColorDefaultMesh;
    }
}   // namespace Exchange